package com.jr.controller;

import com.jr.mapper.ServiceSearchMapper;
import com.jr.pojo.ServiceSearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务搜索控制器
 * 提供基于数据库联查的服务搜索功能
 */
@RestController
@RequestMapping("/api/search")
@CrossOrigin(origins = "*")
public class ServiceSearchController {

    @Autowired
    private ServiceSearchMapper serviceSearchMapper;

    /**
     * 搜索服务
     * 支持服务类型、地区、价格范围三种条件的联查
     * 
     * @param serviceType 服务类型（可选）
     * @param region 地区（可选）
     * @param priceRange 价格范围（可选）
     * @return 搜索结果
     */
    @GetMapping("/services")
    @ResponseBody
    public Map<String, Object> searchServices(
            @RequestParam(required = false) String serviceType,
            @RequestParam(required = false) String region,
            @RequestParam(required = false) String priceRange) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            System.out.println("搜索参数: serviceType=" + serviceType + ", region=" + region + ", priceRange=" + priceRange);
            
            // 执行数据库联查
            List<ServiceSearchResult> services = serviceSearchMapper.searchServices(
                serviceType, region, priceRange);
            
            System.out.println("搜索结果数量: " + services.size());
            
            result.put("success", true);
            result.put("services", services);
            result.put("count", services.size());
            result.put("message", "搜索完成，找到 " + services.size() + " 个服务");
            
        } catch (Exception e) {
            System.err.println("服务搜索失败: " + e.getMessage());
            e.printStackTrace();
            
            result.put("success", false);
            result.put("message", "搜索失败: " + e.getMessage());
            result.put("services", null);
            result.put("count", 0);
        }
        
        return result;
    }

    /**
     * 获取所有活跃的服务类型
     * 
     * @return 服务类型列表
     */
    @GetMapping("/service-types")
    @ResponseBody
    public Map<String, Object> getAllServiceTypes() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            List<String> serviceTypes = serviceSearchMapper.getAllActiveServiceTypes();
            
            result.put("success", true);
            result.put("serviceTypes", serviceTypes);
            result.put("count", serviceTypes.size());
            
        } catch (Exception e) {
            System.err.println("获取服务类型失败: " + e.getMessage());
            e.printStackTrace();
            
            result.put("success", false);
            result.put("message", "获取服务类型失败: " + e.getMessage());
            result.put("serviceTypes", null);
        }
        
        return result;
    }

    /**
     * 获取所有服务区域
     * 
     * @return 服务区域列表
     */
    @GetMapping("/service-areas")
    @ResponseBody
    public Map<String, Object> getAllServiceAreas() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            List<String> serviceAreas = serviceSearchMapper.getAllServiceAreas();
            
            result.put("success", true);
            result.put("serviceAreas", serviceAreas);
            result.put("count", serviceAreas.size());
            
        } catch (Exception e) {
            System.err.println("获取服务区域失败: " + e.getMessage());
            e.printStackTrace();
            
            result.put("success", false);
            result.put("message", "获取服务区域失败: " + e.getMessage());
            result.put("serviceAreas", null);
        }
        
        return result;
    }
}
